package no.la58.scribbles.server.services;

import no.la58.scribbles.client.services.UserService;
import no.la58.scribbles.shared.LoggedInResponse;
import no.la58.scribbles.shared.UserLocation;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class UserServiceImpl extends RemoteServiceServlet implements UserService {

    private static final long serialVersionUID = -1806732775994652845L;

    @Override
    public LoggedInResponse isLoggedIn(UserLocation location) {
	com.google.appengine.api.users.UserService userService = UserServiceFactory
		.getUserService();
	User currentUser = userService.getCurrentUser();
	LoggedInResponse result = new LoggedInResponse();
	if (currentUser != null) {
	    result.setLoggedIn(true);
	    result.setUsermail(currentUser.getNickname());
	    result.setLogInOutUrl(userService.createLogoutURL(location.getCallersUrl()));
	} else {
	    result.setLoggedIn(false);
	    result.setLogInOutUrl(userService.createLoginURL(location.getCallersUrl()));
	}
	return result;
    }

}
